﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Enterprise.Management.Models;

namespace Enterprise.Management.Gridview
{
    public class IntelligentQueryBuilder
    {
        public static List<T> Create<T>(IQueryable query, GridviewPage page)
        {
            IQueryable<T> myQuery = (IQueryable<T>)query;
            page.TotalCount = myQuery.Count();
            int totalPage = page.PageTotal;
            if (page.PageIndex > totalPage)
            {
                page.PageIndex = totalPage > 0 ? totalPage : 1;
            }

            myQuery = myQuery.Skip((page.PageIndex - 1) * page.PageSize).Take(page.PageSize);
            
            return myQuery.ToList();
        }
    }
}